package com.example.springworkadmin.controller;

import com.example.springworkadmin.dao.UserAccountDao;
import com.example.springworkadmin.entity.UserAccount;
import com.example.springworkadmin.util.DescribeCaptchaResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@Controller
public class AdminLoginController {
    @Autowired
    UserAccountDao userAccountDao;

    // 后台-登录界面
    @GetMapping("/admin/login_backend")
    public String adminLogin() {
        return "login_backend";
    }

    // 后台-首页
    @GetMapping("/admin")
    public String adminIndex(Principal principal, HttpServletRequest request) {
        UserAccount userAccount=userAccountDao.getById(principal.getName());
        request.setAttribute("authority",userAccount.getAuthority());
        return "main";
    }

    // 后台-验证码
    @ResponseBody
    @RequestMapping("/CodeServlet")
    public String CodeServlet(String ticket,String randstr){
        System.out.println(ticket);
        System.out.println(randstr);
        if(DescribeCaptchaResult.codeResponse(ticket,randstr)){
            System.out.println("验证成功");
            return "ok";
        }else{
            System.out.println("验证失败");
            return "fail";
        }
    }
}
